3-D Rotation
Volume Number: 1
Issue Number: 13
Column Tag: Lisp Listener
3-D Rotations
By Andy Cohen, Engineer, MacTutor Contributing Editor
Mapping Functions
Applicative operators are functions which use other functions as inputs. One of
the most typical across Lisp dialects is MAPCAR. MAPCAR can perform an operation on
each member of one given list, sequentially. It is therefore another form of iteration.
MAPCAR operates in the same manner APPLY works in the following:
(apply + '(2 3 4 5))
14
MAPCAR, however, provides the capability to perform a given function for each
atom within a given list. It can be seen as an "APPLY_TO_ALL". For example, suppose
one wanted the square root of each value in a list of five values and create a
corresponding list of these square roots. One way to do this is to remove the values
from the list with nested CARS. Then, after applying the SQRT function to each value, a
new list would need to be produced with a CONS. This method might get needlessly
tedious. MAPCAR makes it possible to get this list in a much more convenient fashion.
(MAPCAR (lambda (x) (SQRT x)) '(2 3 4 5))
(1.41421356 1.73205080 2. 2.23606797)
LAMBDA is a special word that tells Lisp that what is following is to be treated as
a function similar to DEFUN. LAMBDA acts like a one time only DEFUN. The (x) is the
passed value and the list with the square root primitive is the expression to be carried
out. MAPCAR takes the first value from the list (2 3 4 5) sequentially (that is why it
is MAPCAR) and places it into x. It then puts each computed value into a list. How about
another sample:
(setq x '(2 5)) (setq y '(5 7))
(mapcar (lambda (n) (* .5 n)) (append x y)))
(2 5 )
(5 7 )
(1. 2.5 2.5 3.5 )
APPEND puts the values represented by the symbols x and y into a list. MAPCAR
takes one atom at a time from the new list and multiplies it by .5.
MAPLIST is another form of mapping or sequencing function. However, instead of
sequencing through a list one atom at a time MAPLIST performs a function on two
entire lists. For example:
(MAPLIST APPEND '(A B C) '(D E F))
((A B C D E F)(B C E F)(C F))
MAPLIST removes the first atom of both lists and performs the function on the